<template>
  <div class="app-container">
    <content-title></content-title>
    <el-form ref="formData" :model="formData" :rules="rules" class="form-container"
             label-width="100px" label-position="right">
      <el-form-item label="路径" prop="path">
        <el-select v-model="formData.path" @change="autoFillOthersByPath" placeholder="选择路径Url">
          <el-option
            v-for="item in allResources"
            :key="item.path"
            :label="item.path"
            :value="item.path">
            <span style="float: left">{{ item.name }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.path }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="父菜单" prop="parentId">
        <el-select v-model="formData.parentId" placeholder="选择父菜单">
          <el-option
            v-for="item in allMenus"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资源名称" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="资源标识" prop="pkey">
        <el-input v-model="formData.pkey"></el-input>
      </el-form-item>
      <el-form-item label="资源类型" prop="type">
        <el-radio-group v-model="formData.type">
          <el-radio name="type" :label="1">菜单</el-radio>
          <el-radio name="type" :label="2">按钮</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <go-back :opt-type="optType"></go-back>
        <el-button type="primary" @click="onSubmit" v-show="optType!==3">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

  import {allUrls, detail, listIdName, save, update} from "@/api/resource";

  export default {
    name: 'resourceDetail',
    props: {
      optType: {
        type: Number,
        default: 3
      }
    },
    data() {
      return {
        allResources: '',
        allMenus: '',
        formData: {
          id: '',
          parentId: '',
          name: '',
          pkey: '',
          path: '',
          type: '',
          sort: '',
        },
        rules: {
          name: [
            {required: true, message: '请输入资源名称', trigger: 'blur'},
          ],
        },
        tempRoute: {}
      }
    },
    computed: {},
    created() {
      this.initOthers();
      this.getDetail();
      this.tempRoute = Object.assign({}, this.$route)
    },
    methods: {
      initOthers() {
        allUrls().then(response => {
          this.allResources = response.data
        })
        listIdName().then(response => {
          this.allMenus = response.data.filter(d => {
            return d.type == 1
          })
          this.allMenus.push({id: "0", name: "根菜单", type: 1})
        })
      },
      getDetail() {
        this.$business.getDetailById(this, detail, (data) => {
          this.formData = data
        })
      },
      onSubmit() {
        this.$business.save(this, save, update, "resourceList");
      },
      autoFillOthersByPath(selectedValue) {
        const filterElement = this.allResources.filter(ress => ress.path === selectedValue)[0];
        this.formData.name = filterElement && filterElement.name;
        const paths = selectedValue.split("/");
        this.formData.pkey = paths[1] + paths[2].slice(0, 1).toUpperCase() + paths[2].slice(1)
        this.formData.type = 2;
        console.log("选中路径", selectedValue);
      }
    }
  }
</script>

